package render.utils
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.textures.Texture;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class TextureUtil
	{
		/**
		 *换算纹理贴图尺寸 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function convertTextureSize(value:int):int
		{
			var tValue:int = 2;
			while(tValue < value)tValue <<= 1;
			return tValue;
		}
		
		/**
		 * 根据位图创建Texture
		 * @param id ID编号
		 * @param bitmapData 位图
		 * @param context3D
		 * @return
		 */
		public static function createTextureByBitmapData(id:String, context3D:Context3D, bitmapData:BitmapData):Texture
		{
			if (bitmapData == null) return null;
			var w:int = TextureUtil.convertTextureSize(bitmapData.width);
			var h:int = TextureUtil.convertTextureSize(bitmapData.height);
			
			var sourceRect:Rectangle = new Rectangle(0, 0, bitmapData.width, bitmapData.height);
			var destPoint:Point = new Point(w * 0.5 - bitmapData.width * 0.5, h * 0.5 - bitmapData.height * 0.5);
			
			var newBmp:BitmapData = new BitmapData(w, h, true, 0x000000);
			newBmp.copyPixels(bitmapData, sourceRect, destPoint);
			var texture:Texture = context3D.createTexture(w, h, Context3DTextureFormat.BGRA, false);
			texture.uploadFromBitmapData(newBmp);
			newBmp.dispose();
			return texture;
		}
		
		/**
		 * 根据已压缩的ByteArray创建Texture
		 * @param id ID编号
		 * @param byteArray 已压缩的ByteArray数据
		 * @param context3D
		 * @return
		 */
		public static function createTextureByCompressedByteArray(id:String, context3D:Context3D, byteArray:ByteArray):Texture
		{
			if (byteArray == null) return null;
			var w:int = Math.pow(2, byteArray[7]);
			var h:int = Math.pow(2, byteArray[8]);
			var textureFormat:String = (byteArray[6] == 2 ? Context3DTextureFormat.COMPRESSED : Context3DTextureFormat.BGRA);
			var texture:Texture = context3D.createTexture(w, h, textureFormat, false);
			texture.uploadCompressedTextureFromByteArray(byteArray, 0, false);
			return texture;
		}
		
		/**
		 * 根据ByteArray创建Texture
		 * @param id ID编号
		 * @param byteArray ByteArray数据
		 * @param context3D
		 * @return
		 */
		public static function createTextureByByteArray(id:String, context3D:Context3D, byteArray:ByteArray):Texture
		{
			if (byteArray == null) return null;
			var w:int = Math.pow(2, byteArray[7]);
			var h:int = Math.pow(2, byteArray[8]);
			var textureFormat:String = (byteArray[6] == 2 ? Context3DTextureFormat.COMPRESSED : Context3DTextureFormat.BGRA);
			var texture:Texture = context3D.createTexture(w, h, textureFormat, false);
			texture.uploadFromByteArray(byteArray, 0);
			return texture;
		}
		
		/**
		 * 根据尺寸创建Texture
		 * @param id ID编号
		 * @param byteArray ByteArray数据
		 * @param context3D
		 * @return
		 */
		public static function createTextureBySize(id:String, context3D:Context3D, width:int, height:int):Texture
		{
			var texture:Texture = context3D.createTexture(width, height, Context3DTextureFormat.BGRA, true);
			return texture;
		}
	}
}